package es.udc.pfc.davidparedes.web.pages.user;

import java.util.Calendar;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.DIRECTOR)
public class DisplayUMProfile {
	
	@Property
	private UniverseManager manager;
	
	@Property
	private String weekHours;
	
	@Property
	private String monthHours;
	
	@Property
	private String trimesterHours;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private Messages messages;
   
    void onActivate(Long id) throws InstanceNotFoundException {
		manager = userService.findUniverseManager(id);
		
		Long[] stats = userService.getManagerStats(manager.getUserProfileId());
		weekHours = formatTime(stats[0]);
		monthHours = formatTime(stats[1]);
		trimesterHours = formatTime(stats[2]);
    }
    
    public String formatDate(Calendar date) {
    	return Integer.toString(date.get(Calendar.DAY_OF_MONTH))
    		+ "/" + Integer.toString((date.get(Calendar.MONTH)+1))
    		+ "/" + Integer.toString(date.get(Calendar.YEAR));
    }
    
    public String formatTime(Long n) {
   		Integer h = (int) (n / (1000*60*60));
   		Integer m = (int) ((n % (1000*60*60)) / (1000*60));
   		String text = h + " " + messages.get("hours");
   		if (m > 0) {
   			text += ", " + m + " " + messages.get("minutes");
   		}
   		return text;
   	}
}
